package com.yss.reportworld.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.yss.common.core.constant.RequestDataHelper;
import com.yss.common.core.enums.YssDataBaseEnum;
import com.yss.common.core.enums.YssStatusEnum;
import com.yss.common.core.exception.BusinessException;
import com.yss.reportworld.domain.rule.RCheckObject;
import com.yss.reportworld.mapper.rule.RCheckObjectMapper;
import com.yss.reportworld.service.IRCheckObjectService;
import com.yss.reportworld.service.IRCheckTypeService;
import com.yss.reportworld.util.TableInfoUtil;
import lombok.extern.slf4j.Slf4j;
import com.yss.common.core.utils.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 数据源配置_校验对象Service业务层处理
 *
 * @author yss
 * @date 2023-07-25
 */
@Slf4j
@Service
public class RCheckObjectServiceImpl extends ServiceImpl<RCheckObjectMapper, RCheckObject> implements IRCheckObjectService {
    @Autowired
    private RCheckObjectMapper rCheckObjectMapper;
    @Autowired
    private IRCheckTypeService rCheckTypeService;

    /**
     * 查询数据源配置_校验对象列表
     *
     * @param rCheckObject 数据源配置_校验对象
     * @return 数据源配置_校验对象
     */
    @Override
    public List<RCheckObject> selectRCheckObjectList(RCheckObject rCheckObject) {
        try {
            List<RCheckObject> list = new ArrayList<>();
            if (StringUtils.isNull(rCheckObject)) {
                return list;
            }
            if (StringUtils.isEmpty(rCheckObject.getRuleTypeId())) {
                return list;
            }
            TableInfoUtil.handleTableVersion(rCheckObject.getHisVersion());
            List<String> ruleTypeIds = new ArrayList<>();
            ruleTypeIds.add(rCheckObject.getRuleTypeId());
            // 获取所有下级的 规则编码
            ruleTypeIds.addAll(rCheckTypeService.queryRCheckTypeIds(rCheckObject.getRuleTypeId()));
            return rCheckObjectMapper.selectExport(YssStatusEnum.NORMAL.getCode(), rCheckObject.getDataBaseType(), null, ruleTypeIds);
        } catch (Exception ex) {
            throw new BusinessException("获取校验对象列表失败，请稍后重试！" + ex.getMessage());
        } finally {
            RequestDataHelper.removeThreadLocal();
        }
    }

}
